package cn.itcast.bos.service.base.impl;

import cn.itcast.bos.dao.base.CourierRepository;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.service.base.CourierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;
import java.util.Set;

@Service
@Transactional
public class CourierServiceImpl implements CourierService {
    //注入dao
    @Autowired
    private CourierRepository courierRepository;

    @Override
    public void save(Courier courier) {
        courierRepository.save(courier);
    }

    //分页查询的条件
    @Override
    public Page<Courier> findPageData(Specification<Courier> specification ,Pageable pageable) {
        return courierRepository.findAll(specification,pageable);
    }

    @Override
    public void markDeleted(Integer[] ids) {
        //调用dao实现update修改，deltag为1；
        for (Integer idstr : ids) {
            courierRepository.updateDeltag(idstr);
        }
    }

    //找到未关联定区的快递员
    @Override
    public List<Courier> findNoAssociation() {
        // 封装Specification
      /*  Specification<Courier> specification = new Specification<Courier>() {
            @Override
            public Predicate toPredicate(Root<Courier> root,
                                         CriteriaQuery<?> query, CriteriaBuilder cb) {
                // 查询条件，判定列表size为空
                Predicate p = cb.isEmpty(root.get("fixedAreas").as(Set.class));
                return p;
            }
        };*/
        return courierRepository.findAll();
    }
}
